package com.qinyh.util;

import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;

import com.qinyh.entity.Point;
import com.qinyh.view.PageView;

public class PathCalculator {

	private Point touchPoint;
	private Point startPoint;
	private Point corner;
	private int flipperType = -1;
	private float width;
	private float height;
	private void setCorner(float x, float y) {
		if (corner == null) {
			corner = new Point();
		}
		corner.setX(x);
		corner.setY(y);
	}
	public int getFlipperType(){
		return flipperType;
	}
	public Point getCorner(){
		return corner;
	}
	public float getAutoScrollX() {
		float y = 1;
		switch (flipperType) {
		case PageView.CENTER_BOTTOM:
			break;
		case PageView.CENTER_LEFT:
			y = 2;
			break;
		case PageView.CENTER_RIGHT:
			y = -2;
			break;
		case PageView.CENTER_TOP:
			break;
		case PageView.LEFT_BOTTOM:
			break;
		case PageView.LEFT_TOP:
			break;
		case PageView.RIGHT_BOTTOM:
			y = -1;
			break;
		case PageView.RIGHT_TOP:
			break;
		}
		return y;
	}

	public float getAutoScrollY() {
		float x = 1;
		switch (flipperType) {
		case PageView.CENTER_BOTTOM:
			break;
		case PageView.CENTER_LEFT:
			x = -2;
			break;
		case PageView.CENTER_RIGHT:
			x = 2;
			break;
		case PageView.CENTER_TOP:
			break;
		case PageView.LEFT_BOTTOM:
			break;
		case PageView.LEFT_TOP:
			break;
		case PageView.RIGHT_BOTTOM:
			x = -2;
			break;
		case PageView.RIGHT_TOP:
			break;
		}
		return x;
	}

	public Point getTouchPoint() {
		return touchPoint;
	}

	public void setTouchPoint(Point touchPoint) {
		this.touchPoint = touchPoint;
	}

	public Point getStartPoint() {
		return startPoint;
	}

	public void setStartPoint(Point startPoint) {
		this.startPoint = startPoint;
	}

	public float getWidth() {
		return width;
	}

	public void setWidth(float width) {
		this.width = width;
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}

	// 当前页path
	public Path getCurrentPath() {
		Path path = new Path();
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			path.lineTo(touchPoint.getX(), 0f);
			path.lineTo(touchPoint.getX(), corner.getY());
			path.lineTo(0f, corner.getY());
			path.lineTo(0, 0);
		}
			break;
		case PageView.CENTER_LEFT: {
			path.moveTo(touchPoint.getX(), 0);
			path.lineTo(width, 0);
			path.lineTo(width, height);
			path.lineTo(touchPoint.getX(), height);
			path.lineTo(touchPoint.getX(), 0);
		}
			break;
		case PageView.RIGHT_BOTTOM:
			path.moveTo(0, 0);
			MNLine k1 = MNLine.initLine(corner.getX(), corner.getY(),
					touchPoint.getX(), touchPoint.getY());
			MNLine k2 = k1.getPBLine((corner.getX() + touchPoint.getX()) / 2,
					(corner.getY() + touchPoint.getY()) / 2);
			MNLine k3 = MNLine.initLine(touchPoint.getX(), touchPoint.getY(),
					width, k2.getYbyX(width));
			if (k3.getYbyX(width) > 0)
				path.lineTo(width, 0);
			path.lineTo(width, k3.getYbyX(width));
			/*RectF oval = new RectF(0,0,200,200);
			path.addArc(oval, 0.5f, 0.7f);*/
			path.lineTo(touchPoint.getX(), touchPoint.getY());
			
			MNLine k4 = k3.getPBLine(touchPoint.getX(), touchPoint.getY());
			
			path.lineTo(k4.getXbyY(height), height);
			path.lineTo(0, height);
			path.lineTo(0, 0);
			break;
		}
		return path;
	}

	// 当前页白色背面的阴影
	public Shader getPageBackPathShader() {
		Shader linearShader = null;
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			linearShader = new LinearGradient(
					(touchPoint.getX() + corner.getX()) / 2,
					corner.getY() * 0.5f, touchPoint.getX(),
					corner.getY() * 0.5f, Color.argb(33, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
			break;
		case PageView.CENTER_LEFT: {
			linearShader = new LinearGradient(touchPoint.getX() / 2,
					corner.getY() * 0.5f, touchPoint.getX(),
					corner.getY() * 0.5f, Color.argb(33, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
		case PageView.RIGHT_BOTTOM: {
			linearShader = new LinearGradient(touchPoint.getX() / 2,
					corner.getY() * 0.5f, touchPoint.getX(),
					corner.getY() * 0.5f, Color.argb(33, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
			break;
		}
		return linearShader;
	}

	// 白色背面
	public Path getPageBackPath() {
		Path path = new Path();
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			path.moveTo(touchPoint.getX(), 0);
			path.lineTo((touchPoint.getX() + corner.getX()) / 2, 0);
			path.lineTo((touchPoint.getX() + corner.getX()) / 2,
					corner.getY() + 0);
			path.lineTo(touchPoint.getX(), corner.getY() + 0);
			path.lineTo(touchPoint.getX(), 0);
		}
			break;
		case PageView.CENTER_LEFT: {
			path.moveTo(touchPoint.getX(), 0f);
			path.lineTo(touchPoint.getX() / 2, 0f);
			path.lineTo(touchPoint.getX() / 2, height);
			path.lineTo(touchPoint.getX(), height);
			path.lineTo(touchPoint.getX(), 0f);
		}
			break;
		case PageView.RIGHT_BOTTOM: {
			MNLine k1 = MNLine.initLine(corner.getX(), corner.getY(),
					touchPoint.getX(), touchPoint.getY());
			MNLine k2 = k1.getPBLine((corner.getX() + touchPoint.getX()) / 2,
					(corner.getY() + touchPoint.getY()) / 2);
			MNLine k3 = MNLine.initLine(touchPoint.getX(), touchPoint.getY(),
					width, k2.getYbyX(width));
			path.moveTo(width, k3.getYbyX(width));
			path.lineTo(touchPoint.getX(), touchPoint.getY());
			MNLine k4 = k3.getPBLine(touchPoint.getX(), touchPoint.getY());
			path.lineTo(k4.getXbyY(height), height);
			path.lineTo(width, k3.getYbyX(width));
		}
			break;
		}
		return path;
	}

	// 翻页方向前面的阴影
	public Shader getForwardShador() {
		Shader linearShader = null;
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			linearShader = new LinearGradient(touchPoint.getX(),
					corner.getY() * 0.50f, touchPoint.getX() - 40,
					corner.getY() * 0.50f, Color.argb(50, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
			//Color.argb(255, 255, 255, 0)
		}
			break;
		case PageView.CENTER_LEFT: {
			linearShader = new LinearGradient(touchPoint.getX(),
					corner.getY() * 0.50f, touchPoint.getX() + 20,
					corner.getY() * 0.50f, Color.argb(20, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
			break;
		case PageView.RIGHT_BOTTOM:
		{
			linearShader = new LinearGradient(corner.getX()-20,
					corner.getY()-20, touchPoint.getX(),
					touchPoint.getY(), Color.argb(20, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
			break;
		}
		return linearShader;
	}

	// 翻页方向后面的阴影
	public Shader getBackShador() {
		Shader linearShader = null;
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			linearShader = new LinearGradient(
					(touchPoint.getX() + corner.getX()) / 2,
					corner.getY() * 0.50f,
					(touchPoint.getX() + corner.getX()) / 2 + 40,
					corner.getY() * 0.50f, Color.argb(33, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
			break;
		case PageView.CENTER_LEFT: {
			linearShader = new LinearGradient(touchPoint.getX() / 2,
					corner.getY() * 0.50f, touchPoint.getX() / 2 - 40,
					corner.getY() * 0.50f, Color.argb(33, 0, 0, 0), Color.argb(
							0, 0, 0, 0), TileMode.CLAMP);
		}
			break;
		case PageView.RIGHT_BOTTOM: {
			MNLine k1 = MNLine.initLine(corner.getX(), corner.getY(),
					touchPoint.getX(), touchPoint.getY());
			MNLine k2 = k1.getPBLine((corner.getX() + touchPoint.getX()) / 2,
					(corner.getY() + touchPoint.getY()) / 2);
			float y1 = k2.getYbyX(width);
			float x1 = k2.getXbyY(height);
			MNLine k3 = k2.move(20f, 0f);
			float y2 = k3.getYbyX(width);
			float x2 = k3.getXbyY(height);
			linearShader = new LinearGradient((x1 + width) / 2,
					(y1 + height) / 2, (x2 + width) / 2, (y2 + width) / 2,
					Color.argb(33, 0, 0, 0), Color.argb(0, 0, 0, 0),
					TileMode.CLAMP);
		}
			break;
		}
		return linearShader;
	}

	// 翻页方向的前方的阴影路径
	public Path getForwardShadorPath() {
		Path path = new Path();
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			path.moveTo(touchPoint.getX() -(width-touchPoint.getX())*0.25f, 0f);
			path.lineTo(touchPoint.getX(), 0f);
			path.lineTo(touchPoint.getX(), corner.getY());
			path.lineTo(touchPoint.getX() - (width-touchPoint.getX())*0.25f, corner.getY());
			path.lineTo(touchPoint.getX() - (width-touchPoint.getX())*0.25f, 0f);
		}
			break;
		case PageView.CENTER_LEFT: {
			path.moveTo(touchPoint.getX(), 0f);
			path.lineTo(touchPoint.getX() * 1.25f, 0f);
			path.lineTo(touchPoint.getX() *1.25f, corner.getY());
			path.lineTo(touchPoint.getX(), corner.getY());
			path.lineTo(touchPoint.getX(), 0f);
		}
			break;
		case PageView.RIGHT_BOTTOM:
			MNLine k1 = MNLine.initLine(corner.getX(), corner.getY(),
					touchPoint.getX(), touchPoint.getY());
			MNLine k2 = k1.getPBLine((corner.getX() + touchPoint.getX()) / 2,
					(corner.getY() + touchPoint.getY()) / 2);
			MNLine k3 = MNLine.initLine(touchPoint.getX(), touchPoint.getY(), width, k2.getYbyX(width));//内层较上直线
			MNLine k4 = k3.getPBLine(touchPoint.getX(),touchPoint.getY());//内层较下直线
			MNLine k5 = MNLine.initLine(k3.getK(), touchPoint.getX()-15,k3.getYbyX(touchPoint.getX()-5)-15);//外层较上直线
			MNLine k6 = MNLine.initLine(k4.getK(), touchPoint.getX()-15,k3.getYbyX(touchPoint.getX()-5)-15);//外层较下直线
			path.moveTo(width, k5.getYbyX(width));
			path.lineTo(k5.getCross(k6)[0], k5.getCross(k6)[1]);
			path.lineTo(k6.getXbyY(height), height);
			path.lineTo(k4.getXbyY(height), height);
			path.lineTo(k3.getCross(k4)[0], k3.getCross(k4)[1]);
			path.lineTo(width, k3.getYbyX(width));
			break;
		}
		return path;
	}

	// 翻页方向的后方的阴影路径
	public Path getBackShadorPath() {
		Path path = new Path();
		switch (flipperType) {
		case PageView.CENTER_RIGHT: {
			path.moveTo((touchPoint.getX() + corner.getX()) / 2, 0f);
			path.lineTo((touchPoint.getX() + corner.getX()) / 2 + 40, 0f);
			path.lineTo((touchPoint.getX() + corner.getX()) / 2 + 40,
					corner.getY());
			path.lineTo((touchPoint.getX() + corner.getX()) / 2, corner.getY());
			path.lineTo((touchPoint.getX() + corner.getX()) / 2, 0f);
		}
			break;
		case PageView.CENTER_LEFT: {
			path.moveTo(touchPoint.getX() / 2, 0f);
			path.lineTo(touchPoint.getX() / 2 - 40, 0f);
			path.lineTo(touchPoint.getX() / 2 - 40, corner.getY() * 1.5f);
			path.lineTo(touchPoint.getX() / 2, corner.getY() * 1.5f);
			path.lineTo(touchPoint.getX() / 2, 0f);
		}
			break;
		case PageView.RIGHT_BOTTOM: {
			MNLine k1 = MNLine.initLine(corner.getX(), corner.getY(),
					touchPoint.getX(), touchPoint.getY());
			MNLine k2 = k1.getPBLine((corner.getX() + touchPoint.getX()) / 2,
					(corner.getY() + touchPoint.getY()) / 2);
			path.moveTo(width, k2.getYbyX(width));
			path.lineTo(k2.getXbyY(height), height);
			MNLine k3 = k2.move(20f, 0f);
			path.lineTo(k3.getXbyY(height), height);
			path.lineTo(width, k3.getYbyX(width));
		}
			break;
		}
		return path;
	}

	// 从那个角落翻
	public void addFlipperType() {
		int x = (int) (startPoint.getX() * 3 / this.getWidth());
		int y = (int) (startPoint.getY() * 3 / this.getHeight());
		switch (x) {
		case 0:
			switch (y) {
			case 0:
				flipperType = PageView.LEFT_TOP;
				setCorner(0, 0);
				break;
			case 1:
				flipperType = PageView.CENTER_LEFT;
				setCorner(0, height);
				break;
			case 2:
				flipperType = PageView.LEFT_BOTTOM;
				setCorner(width, 0);
				break;
			}
			break;
		case 1:
			switch (y) {
			case 0:
				flipperType = PageView.CENTER_TOP;
				break;
			case 2:
				flipperType = PageView.CENTER_BOTTOM;
				break;
			}
			break;
		case 2:
			switch (y) {
			case 0:
				flipperType = PageView.RIGHT_TOP;
				setCorner(0, height);
				break;
			case 1:
				flipperType = PageView.CENTER_RIGHT;
				setCorner(width, height);
				break;
			case 2:
				flipperType = PageView.RIGHT_BOTTOM;
				setCorner(width, height);
				break;
			}
			break;
		}

	}
}
